﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using CE.iPhone.PList;
namespace cczEdit.DramaDialog
{
    public partial class DramaConvetDialog : Form
    {
        private FolderBrowserDialog folderBrowserDialog;
        private PListRoot currentFile;
        private UCDrama parentUserControl;
        public DramaConvetDialog()
        {
            InitializeComponent();
        }
        public DialogResult Show(PListRoot root, IWin32Window owner,UCDrama parent)
        {
            currentFile = root;
            parentUserControl = parent;
            return this.ShowDialog(owner);
        }
        private void buttonOriginFolder_Click(object sender, EventArgs e)
        {
            folderBrowserDialog = new FolderBrowserDialog();
            folderBrowserDialog.RootFolder = Environment.SpecialFolder.Desktop;
            if (folderBrowserDialog.ShowDialog(this) == DialogResult.OK)
            {
                textBoxOriginPath.Text = folderBrowserDialog.SelectedPath;
                
            }
        }

        private void buttonTargetFolder_Click(object sender, EventArgs e)
        {
            folderBrowserDialog = new FolderBrowserDialog();
            folderBrowserDialog.RootFolder = Environment.SpecialFolder.Desktop;
            if (folderBrowserDialog.ShowDialog(this) == DialogResult.OK)
            {
                textBoxTargetPath.Text = folderBrowserDialog.SelectedPath;

            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (textBoxNum.Text == "" || textBoxOriginPath.Text == "" || textBoxTargetPath.Text == "")
            {
                MessageBox.Show("请填写路径以及剧本个数");
                return;
            }
            labelProcState.Text = "转换中...";

            string targetFileName = textBoxTargetPath.Text + (radioButtonRDrama.Checked ? "\\RDrama" : "\\SDrama");
            string originFileName = textBoxOriginPath.Text + (radioButtonRDrama.Checked ? "\\R_" : "\\S_");
            int num = int.Parse(textBoxNum.Text);
            progressBar1.Maximum = num;//设置最大长度值
           progressBar1.Value = 0;//设置当前值
           progressBar1.Step = 1;//设置没次增长多少
            for (int i = 0; i < num; ++i)
            {
                string realOriginFileName = originFileName + (i < 10 ? ("0" + i) : "" + i) + ".eex";
                string realTargetFileName = targetFileName + i + ".plist";
                if (radioButtonRDrama.Checked)
                {
                    currentFile = new PListRoot();
                    PListDict rootDic = new PListDict();
                    currentFile.Root = rootDic;
                    //Story
                    PListArray story = new PListArray();
                    rootDic.Add("Story", story);
                    //Council
                    PListArray council = new PListArray();
                    rootDic.Add("Council", council);
                    //SpecialStory
                    story = new PListArray();
                    rootDic.Add("SpecialStory", story);
                }
                else
                {
                    currentFile = new PListRoot();
                    PListDict rootDic = new PListDict();
                    currentFile.Root = rootDic;
                    //Story
                    PListArray story = new PListArray();
                    rootDic.Add("Story", story);
                    //Trigger
                    PListArray trigger = new PListArray();
                    rootDic.Add("Trigger", trigger);
                    //PreSetting
                    PListArray preSetting = new PListArray();
                    rootDic.Add("PreSetting", preSetting);
                    preSetting.Add(AddDramaDialog.CreateInstructDict(kInstructType.kInstructTypeSetPlaceName));
                    preSetting.Add(AddDramaDialog.CreateInstructDict(kInstructType.kInstructTypeSetGlobalVar));
                    preSetting.Add(AddDramaDialog.CreateInstructDict(kInstructType.kInstructTypeSetPlayerBattle));
                    preSetting.Add(AddDramaDialog.CreateInstructDict(kInstructType.kInstructTypeSetEnemyBattle));
                    preSetting.Add(AddDramaDialog.CreateInstructDict(kInstructType.kInstructTypeSetFriendBattle));
                    //LastSetting
                    PListDict lastSetting = new PListDict();
                    rootDic.Add("LastSetting", lastSetting);
                    lastSetting.Add("Information", new PListString("战后处置"));

                }
                parentUserControl.dramaFileData.LoadData(realOriginFileName, (PListDict)currentFile.Root);
                PListDict curRootDic = (PListDict)currentFile.Root;
                if (radioButtonRDrama.Checked)
                {
                    PListArray curCouncilArr = (PListArray)curRootDic["Council"];
                    if (curCouncilArr.Count > 0)
                        curCouncilArr.RemoveAt(0);
                }
                else
                {
                    PListArray curSceneArr = (PListArray)curRootDic["Story"];
                    if (curSceneArr.Count > 1)
                    {
                        PListDict lastScene = (PListDict)curSceneArr[curSceneArr.Count - 1];
                        PListDict lastSetting = (PListDict)curRootDic["LastSetting"];
                        lastSetting["Dramas"] = lastScene["Dramas"];
                        curSceneArr.Remove(lastScene);

                    }
                    PListDict sceneDict = (PListDict)curSceneArr[0];
                    PListArray dramasArr = (PListArray)sceneDict["Dramas"];
                    PListDict dictWillRemoved=null;
                    foreach (PListDict dramaDict in dramasArr)
                    {
                        PListInteger instructID = (PListInteger)dramaDict["InstructType"];
                        if (instructID.Value == (long)kInstructType.kInstructTypeConditionTest)
                        {
                            PListString dspt = (PListString)dramaDict["Information"];
                            if (dspt == "HEX我军设定")
                            {
                                dictWillRemoved = dramaDict;
                            }
                        }
                    }
                    if (dictWillRemoved != null)
                        dramasArr.Remove(dictWillRemoved);
                }
                currentFile.Save(realTargetFileName, PListFormat.Xml);
                labelProcState.Text = "转换中：" + (i + 1) + " / " + num ;

                if (progressBar1.Value<num)
                    progressBar1.Value += progressBar1.Step;
                
            }
            labelProcState.Text = "转换完成";
        }
    }
}
